// 时间格式化
export function getdate(d, typeM) {
	var date = new Date();
	if (d) {
		date = new Date(d);
	}
	var year = date.getFullYear();
	var month = init(date.getMonth() + 1);
	var day = init(date.getDate());
	var H = init(date.getHours());
	var M = init(date.getMinutes());
	var S = init(date.getSeconds());
	var m = init(date.getMilliseconds());
	let res = typeM == 'm' ? `${year}-${month}-${day} ${H}:${M}:${S}:${m}` : `${year}-${month}-${day} ${H}:${M}:${S}`

	return res;
}
//补零
function init(n) {
	return n < 10 ? '0' + n : n;
}

//判断两个日期是否一样 
export function DateIfSimilar(begind, endd) {
	var beginDate = new Date(begind);
	var endDate = new Date(endd);
	if (beginDate.getFullYear() != endDate.getFullYear()) {
		return false;
	}
	if (beginDate.getMonth() != endDate.getMonth()) {
		return false;
	}
	if (beginDate.getDate() != endDate.getDate()) {
		return false;
	}
	return true;
}

//返回时分秒 
export function formatDateHMS(d) {
	var date = new Date(d);
	var H = init(date.getHours());
	var M = init(date.getMinutes());
	var S = init(date.getSeconds());
	return `${H}:${M}:${S}`;
}
export function formatTime(time, option) {
	// time = +time * 1000
	const d = new Date(time)
	const now = Date.now()
	const diff = (now - d) / 1000
	if (diff < 30) {
		return '刚刚'
	} else if (diff < 3600) { // less 1 hour
		return Math.ceil(diff / 60) + '分钟前'
	} else if (diff < 3600 * 24) {
		return Math.ceil(diff / 3600) + '小时前'
	} else if (diff < 3600 * 24 * 2) {
		return '1天前'
	}
	if (option) {
		return parseTime(time, option)
	} else {
		return d.getMonth() + 1 + '月' + d.getDate() + '日' + d.getHours() + '时' + d.getMinutes() + '分'
	}
}


/**
 * @param {Number|null} 
 * 获取年份的每月天数列表 默认本年
 */
function format_year_month_count(year = new Date().getFullYear()) {
	let list = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
	if (year % 100 != 0 && year % 4 == 0 || year % 400 == 0) {
		list[1] = 29
	}
	return list
}
/**
 * @param date
 * 记录列表初始化
 */
const weekList = "日一二三四五六".split("")
export function format_init(date = new Date()) {
	date = date || new Date()
	let year = date.getFullYear(); //年份
	let month = date.getMonth() + 1; //月份
	let day = date.getDate(); //日
	let week = date.getDay(); //星期几
	// daysCount= new Date(year, month, 0).getDate();
	let daysCount = format_year_month_count(year)[month - 1]; //获取月份的天数
	let firstWeek = new Date(year, month - 1, 1).getDay() //获取第一天是星期几
	let lastWeek = new Date(year, month - 1, daysCount).getDay() //获取最后一天是星期几
	let lastDayscount = getLastDayscount(year, month); //获取上个月有几天
	let list_days = [] //天数列表
	let firstCount = 0,
		lastCount = 0;
	if (firstWeek == 0) {
		for (var i = lastDayscount; i > lastDayscount - 6; i--) {
			firstCount += 1
			list_days.unshift(i)
		}
	} else {
		for (var i = lastDayscount; i > lastDayscount - firstWeek + 1; i--) {
			firstCount += 1
			list_days.unshift(i)
		}
	}

	for (let i = 1; i <= daysCount; i++) {
		list_days.push(i)
	}

	for (var i = 1; i <= 7 - lastWeek; i++) {
		lastCount += 1
		list_days.push(i)
	}
	return {
		year,
		month,
		day,
		daysCount,
		lastDayscount,
		list_days,
		week,
		firstWeek,
		lastWeek,
		weekStr: weekList[week],
		firstCount,
		lastCount
	}
}
//获取上个月有几天
function getLastDayscount(year, month) {
	let m = month - 2;
	if (m < 0) {
		year -= 1
		m = 11
	}
	return format_year_month_count(year)[m];
}




/**
 * 切割字符串 赋值数组
 * @param {string|Array} value 字符串||数组
 * @param {string} key 键
 * @param {string} [code=',']  分隔符 default：,
 * @return {Array} value [] 数组
 **/
export function handledSHSArray(value, key, code = ',') {
	if (!value) return []
	let obj=[]
	if (key) {
		if (typeof value[key] == "string" && value[key].trim() != "") {
			obj[key]= value[key].split(code)
		} else {
			obj[key]= value[key] || []
		}
		return obj[key]
	} else {
		if (typeof value == "string") {
			obj = value.split(code)
		}
	}
	return obj
}
 const oneDay = 24 * 60 * 60 * 1000; // 每天的毫秒数
export function daysBetween(date1, date2) {
  const firstDate = new Date(date1);
  const secondDate = new Date(date2);
  // 计算时间差
  const diff = Math.abs(firstDate - secondDate);
 
  // 返回差值的天数
  return Math.round(diff / oneDay + 1);
}




export function parseTime(time, cFormat) {
	if (arguments.length === 0) {
		return null
	}
	const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
	let date
	if (typeof time === 'object') {
		date = time
	} else {
		if (('' + time).length === 10) time = parseInt(time) * 1000
		date = new Date(time)
	}
	const formatObj = {
		y: date.getFullYear(),
		m: date.getMonth() + 1,
		d: date.getDate(),
		h: date.getHours(),
		i: date.getMinutes(),
		s: date.getSeconds(),
		a: date.getDay()
	}
	const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
		let value = formatObj[key]
		if (key === 'a') return ['一', '二', '三', '四', '五', '六', '日'][value - 1]
		if (result.length > 0 && value < 10) {
			value = '0' + value
		}
		return value || 0
	})
	return time_str
}

export function getType(value) {
	if (value === null) return String(value)
	return typeof value === 'object' ? Object.prototype.toString.call(value).replace('[object ', '').replace(']', '')
		.toLowerCase() : typeof value
}

export function deepClone(target) {
	if(target == null) return target;
	if(target instanceof Date) return new Date(target);
	if(target instanceof RegExp) return new RegExp(target);
	if (typeof target !== "object") return target
	
	let cloneTarget = new target.constructor;
	for (let key in target) {
		if (target.hasOwnProperty(key)) {
			cloneTarget[key] = deepClone(target[key]);
		}
	}
	return cloneTarget;
}
 
